package com.sabrebutt.pongur;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.provider.Settings;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.WindowManager;

/**
 * Android Live Wallpaper painting thread Archetype
 * 
 * @author antoine vianey GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
 * 
 *         Used to create PongUr written by Cal Brower
 * 
 */
public class PongUrPaint extends Thread {

	private enum HorizontalDirection {
		LEFT, RIGHT
	}

	private enum VerticalDirection {
		UP, DOWN
	}

	public static final String SHARED_PREFS_NAME = "pongursettings";

	/** Reference to the View and the context */
	private SurfaceHolder surfaceHolder;
	private Context context;
	/** State */
	private boolean wait;
	private boolean run;
	private boolean military;
	private boolean showDash;
	private int score1;
	private int score2;
	private Calendar cal;
	private SimpleDateFormat sdf;
	private Random rand;
	private String time;
	private int hour;
	private int minute;
	private Rect ball;
	private Point ballPos;
	private Point p1Pos;
	private Point p2Pos;
	private Point textPos;
	private Point textPos2;
	private Rect p1;
	private Rect p2;
	private Paint paint;
	private Paint datePaint;
	private int bShape;
	private int bColor;
	private int tap2;
	private int tap;
	private int mTouchX;
	private int mTouchY;

	private double speedy;

	private double speedx;

	private HorizontalDirection myXDirection;
	private VerticalDirection myYDirection;

	/** Dimensions */
	private int width;
	private int height;

	/** Time tracking */
	private long previousTime;
	private long currentTime;

	private int paddlespeed;

	private int speedChoice;

	private Typeface face;

	private SimpleDateFormat date;

	private String dateString;

	private int ballHalf;

	private int ballSize;

	private Display display;

	private String color;

	private SharedPreferences mPrefs;

	private RectF p1f;

	private RectF p2f;

	private SimpleDateFormat aprfir;

	private String stil;

	private boolean fool;

	private int bgColor;

	private int nTouchX;

	private boolean gameplay;

	private boolean showclock;

	private Path dashPath;

	private int widthHalf;

	private int heightHalf;

	private int height20;

	private float ballQuarter;

	private int paddleWidth;

	private int mColorDiv;

	private int paddleHalf;

	private int widthQuarter;

	private int frame;

	private double gopher;

	private double gopherb;

	private boolean showShadow;

	public PongUrPaint(SurfaceHolder surfaceHolder, Context context,
			SharedPreferences mPrefs) {
		this.frame = 0;
		this.mPrefs = mPrefs;
		display = ((WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		this.width = display.getWidth();
		this.height = display.getHeight();
		this.widthHalf = width / 2;
		this.widthQuarter = widthHalf / 2;
		this.mColorDiv = 2;
		this.heightHalf = height / 2;
		this.height20 = height / 20;
		this.ballSize = width / 20;
		this.paddleWidth = ballSize * 3;
		this.ballHalf = ballSize / 2;
		this.ballQuarter = ballHalf / 2;
		this.gopher = widthHalf;
		this.gopherb = widthHalf;
		this.rand = new Random();
		this.speedChoice = 2;
		String speed = mPrefs.getString("pongur_speed", "2");
		setSpeed(speed);
		String timeSetting = Settings.System.getString(context
				.getContentResolver(), Settings.System.TIME_12_24);
		this.military = (timeSetting == null) ? false : (timeSetting
				.equals("12") ? false : true);
		this.gameplay = mPrefs.getBoolean("gameplay", false);
		this.showclock = mPrefs.getBoolean("showclock", true);
		this.showShadow = mPrefs.getBoolean("showshadow", false);
		this.showDash = mPrefs.getBoolean("showdash", true);
		this.cal = Calendar.getInstance();
		this.sdf = new SimpleDateFormat("HH:mm");
		this.time = sdf.format(cal.getTime());
		this.aprfir = new SimpleDateFormat("ddMM");
		this.stil = aprfir.format(cal.getTime());
		this.fool = ((Integer.parseInt(stil.substring(0, 2)) == 1) && (Integer
				.parseInt(stil.substring(2, 4)) == 4));
		this.hour = (military) ? Integer.parseInt(time.substring(0, 2))
				: (Integer.parseInt(time.substring(0, 2))) % 12;
		if (hour == 0 && !military)
			hour = 12;
		this.minute = Integer.parseInt(time.substring(3, 5));
		this.date = new SimpleDateFormat("EEEEEEEEE, dd MMMMMMMM");
		this.dateString = date.format(cal.getTime());
		this.surfaceHolder = surfaceHolder;
		this.context = context;
		this.score1 = hour;
		this.score2 = minute;
		this.paint = new Paint();
		this.speedx = rand.nextInt(4 * speedChoice) + 4;
		this.speedy = rand.nextInt(4 * speedChoice) + 4;
		this.paddlespeed = speedChoice + (width / 48);
		this.bgColor = 0xffffff;
		this.ballPos = new Point(((widthHalf) - ballHalf), heightHalf);
		this.p1Pos = new Point(widthHalf, height20);
		this.p2Pos = new Point(widthHalf, height - (width / 5));
		this.ball = new Rect(ballPos.x, ballPos.y, ballPos.x + ballSize,
				ballPos.y + ballSize);
		this.p1 = new Rect(p1Pos.x, p1Pos.y, p1Pos.x + paddleWidth, p1Pos.y
				+ (ballSize));
		this.p2 = new Rect(p2Pos.x, p2Pos.y, p2Pos.x + paddleWidth, p2Pos.y
				+ (ballSize));

		String scorePos = mPrefs.getString("pongur_scorepos", "two");
		this.color = mPrefs.getString("pongur_color", "black");

		this.bShape = (mPrefs.getString("pongur_shape", "cube").equals("cube")) ? 0
				: 1;
		this.textPos = new Point();
		this.textPos2 = new Point();
		this.textPos.x = widthHalf;
		this.textPos2.x = widthHalf;
		setScorePos(scorePos);
		this.face = Typeface.createFromAsset(context.getAssets(),
				"ARCADE_N.TTF");
		this.paint.setTextAlign(Align.CENTER);
		this.paint.setAntiAlias(true);
		this.datePaint = new Paint();
		if (bShape == 0) {
			paint.setTypeface(face);
			paint.setTextSize(48);
			datePaint.set(paint);
			datePaint.setTextSize(14);
		} else {
			paint.setTypeface(Typeface.MONOSPACE);
			paint.setTextSize(72);
			datePaint.set(paint);
			datePaint.setTextSize(20);
		}
		setColor(color);
		this.datePaint.setColor(paint.getColor());
		this.mTouchX = mTouchY = -1;
		this.myXDirection = HorizontalDirection.RIGHT;
		this.myYDirection = VerticalDirection.UP;
		this.dashPath = new Path();
		for (int i = 0; i < width; i += width / 30) {
			dashPath.addRect((((i) * 2) + (width / 48) / 2),
					((heightHalf) - ballHalf), (((i) * 2) + (width / 24)),
					(heightHalf) + ballHalf, Path.Direction.CW);
		}
		this.wait = true;
	}

	/**
	 * Do the actual drawing stuff
	 */
	private void doDraw(Canvas canvas) {
		if (frame % 64 == 0 && showclock) {
			cal = Calendar.getInstance();
			time = sdf.format(cal.getTime());
			minute = Integer.parseInt(time.substring(3, 5));
			hour = (military) ? Integer.parseInt(time.substring(0, 2))
					: (Integer.parseInt(time.substring(0, 2))) % 12;
			if (hour == 0 && !military)
				hour = 12;
			this.dateString = date.format(cal.getTime());
		}
		if (Rect.intersects(ball, p1)) {
			this.myYDirection = VerticalDirection.DOWN;
			this.speedx = rand.nextInt(4 * speedChoice) + 4 + (0.08 * tap2);
			this.speedy = rand.nextInt(4 * speedChoice) + 4 + (0.08 * tap2);
			tap++;
			tap2++;
		}
		if (Rect.intersects(ball, p2)) {
			this.myYDirection = VerticalDirection.UP;
			this.speedx = rand.nextInt(4 * speedChoice) + 6 + (0.08 * tap2);
			this.speedy = rand.nextInt(4 * speedChoice) + 6 + (0.08 * tap2);
			tap++;
			tap2++;
		}
		// x
		if (ballPos.x >= width - ball.width()) {
			this.myXDirection = HorizontalDirection.LEFT;
		} else if (ballPos.x <= 0) {
			this.myXDirection = HorizontalDirection.RIGHT;
		}

		// y
		if (ballPos.y >= height - ball.height()) {
			if (showclock) {
				score1++;
				tap2 = 0;
				ballPos = new Point(widthHalf, heightHalf);
				this.speedx = rand.nextInt(4 * speedChoice) + 4;
				this.speedy = rand.nextInt(4 * speedChoice) + 4;
				if (score1 != hour + 1)
					score1 = (gameplay) ? ((nTouchX > 1) ? score1 : hour)
							: hour;
				if (score1 > ((military) ? 23 : 12))
					score1 = (military) ? 0 : 1;
			} else {
				score1 = hour;
				tap2 = 0;
				ballPos = new Point(widthHalf, heightHalf);
				this.speedx = rand.nextInt(4 * speedChoice) + 4;
				this.speedy = rand.nextInt(4 * speedChoice) + 4;
			}

		} else if (ballPos.y <= 0) {
			if (showclock) {
			score2++;
			tap2 = 0;
			ballPos = new Point(widthHalf, heightHalf);
			this.speedx = rand.nextInt(4 * speedChoice) + 4;
			this.speedy = rand.nextInt(4 * speedChoice) + 4;
			if (!(score2 % 55 < (minute + 5) % 60))
				score2 = (gameplay) ? ((nTouchX > 1) ? 1000 : minute) : minute;
			if (score2 > 59)
				score2 = 0;
			} else {
				score2 = minute;
				tap2 = 0;
				ballPos = new Point(widthHalf, heightHalf);
				this.speedx = rand.nextInt(4 * speedChoice) + 4;
				this.speedy = rand.nextInt(4 * speedChoice) + 4;
			}
		}

		/* Move the ball left or right */
		if (this.myXDirection == HorizontalDirection.RIGHT) {
			this.ballPos.x += speedx;
		} else {
			this.ballPos.x -= speedx;
		}
		/* Move the ball up or down */
		if (this.myYDirection == VerticalDirection.DOWN) {
			this.ballPos.y += speedy;
			if (frame % 12 == 0) {
				gopher = widthHalf;
			}
			if (p1Pos.x + paddleHalf < gopher) {
				this.p1Pos.x += (p1Pos.x + paddleWidth - 4 <= width
						- (paddlespeed - 2)) ? paddlespeed : 0;
			} else if (p1Pos.x > gopher) {
				this.p1Pos.x -= (p1Pos.x - (paddlespeed - 2) >= 0) ? paddlespeed
						: 0;

			}
			int distyb = p2Pos.y - ballPos.y;
			if (nTouchX < 1 || nTouchX > width - ballSize) {
				if (frame % 12 == 0) {
					gopherb = (this.myXDirection == HorizontalDirection.LEFT) ? Math
							.abs(ballPos.x - ((distyb / speedy) * speedx))
							: width
									- Math.abs((width - (ballPos.x + distyb
											/ speedy * speedx)));
				}
			} else {
				gopherb = nTouchX;
			}
			if (hour == score1) {
				if (p2Pos.x < gopherb - paddleHalf) {
					this.p2Pos.x += (p2Pos.x + paddleWidth - 4 <= width
							- (paddlespeed - 2)) ? paddlespeed : 0;
				} else if (p2Pos.x > gopherb)
					this.p2Pos.x -= (p2Pos.x - (paddlespeed - 2) >= 0) ? paddlespeed
							: 0;
			} else {
				if (gameplay && !(nTouchX < 1 || nTouchX > width)) {
					if (p2Pos.x < gopherb - paddleHalf) {
						this.p2Pos.x += (p2Pos.x + paddleWidth - 4 <= width
								- (paddlespeed - 2)) ? paddlespeed : 0;
					} else if (p2Pos.x > gopherb)
						this.p2Pos.x -= (p2Pos.x - (paddlespeed - 2) >= 0) ? paddlespeed
								: 0;

				} else {
					if (gopherb < widthHalf) {
						if (p2Pos.x - width / 10 < widthHalf)
							this.p2Pos.x += (p2Pos.x + paddleWidth <= width
									- (paddlespeed - 2)) ? paddlespeed : 0;
					} else {
						if (p2Pos.x + widthQuarter > widthHalf)
							this.p2Pos.x -= (p2Pos.x - (paddlespeed - 2) >= 0) ? paddlespeed
									: 0;
					}
				}
			}
		} else {
			this.ballPos.y -= speedy;
			if (nTouchX < 1 || nTouchX > width) {
				gopherb = widthHalf;
			} else {
				gopherb = nTouchX;
			}
			if (p2Pos.x + paddleWidth < gopherb) {
				this.p2Pos.x += (p2Pos.x + paddleWidth - 4 <= width
						- (paddlespeed - 2)) ? paddlespeed : 0;
			} else if (p2Pos.x > gopherb)
				this.p2Pos.x -= (p2Pos.x - (paddlespeed - 2) >= 0) ? paddlespeed
						: 0;
			if (frame % 12 == 0) {
				int disty = ballPos.y + ballSize - (p1Pos.y + ballSize);
				gopher = (this.myXDirection == HorizontalDirection.LEFT) ? Math
						.abs(ballPos.x - ((disty / speedy) * speedx))
						: (width - Math
								.abs((width - (ballPos.x + ((disty / speedy) * speedx)))));
			}
			if (minute == score2) {
				if (p1Pos.x < gopher - paddleHalf) {
					this.p1Pos.x += (p1Pos.x + paddleWidth <= width
							- (paddlespeed - 2)) ? paddlespeed : 0;
				} else if (p1Pos.x > gopher) {
					this.p1Pos.x -= (p1Pos.x - (paddlespeed - 2) >= 0) ? paddlespeed
							: 0;

				}
			} else {
				if (gopher < widthHalf) {
					if (p1Pos.x - width / 10 < widthHalf)
						this.p1Pos.x += (p1Pos.x + paddleWidth <= width
								- (paddlespeed - 2)) ? paddlespeed : 0;
				} else {
					if (p1Pos.x + widthQuarter > widthHalf)
						this.p1Pos.x -= (p1Pos.x - (paddlespeed - 2) >= 0) ? paddlespeed
								: 0;
				}
			}
		}
		if (canvas == null) {

		} else {
			if ((tap > 1000000) || (fool) || color.equals("apr")) {
				color = "apr";
				setColor(color);
				this.ball = new Rect(ballPos.x, ballPos.y,
						ballPos.x + ballSize, ballPos.y + ballSize);
				canvas.drawColor(bgColor);
			} else if (color.equals("b")) {
				setColor(color);
				canvas.drawColor(bgColor);
			} else {
				canvas.drawColor(bColor);
			}
			if (showDash)
				canvas.drawPath(dashPath, paint);
			ball.offsetTo(ballPos.x, ballPos.y);
			p1.offsetTo(p1Pos.x, p1Pos.y);
			p2.offsetTo(p2Pos.x, p2Pos.y);
			if (bShape == 0) {
				canvas.drawRect(ball, paint);
				canvas.drawRect(p1, paint);
				canvas.drawRect(p2, paint);
			} else {
				p1f = new RectF(p1);
				p2f = new RectF(p2);
				canvas.drawCircle(ballPos.x + ballHalf, ballPos.y + ballHalf,
						ballHalf, paint);
				canvas.drawRoundRect(p1f, ballQuarter, ballQuarter, paint);
				canvas.drawRoundRect(p2f, ballQuarter, ballQuarter, paint);
			}
			if (showclock) {
				canvas.drawText("" + ((score1 < 10) ? "0" + score1 : score1)
						+ ":" + ((score2 < 10) ? "0" + score2 : score2),
						textPos.x, textPos.y, paint);
				canvas.drawText(dateString, textPos2.x, textPos2.y, datePaint);
			}
			if (mTouchX >= 0 && mTouchY >= 0) {
				ballPos = new Point(mTouchX, mTouchY);
			}
			frame++;
		}
	}

	public void doTouchEvent(MotionEvent event) {
		synchronized (this) {
			if (event.getAction() == MotionEvent.ACTION_MOVE) {
				if ((event.getY() > (height20) + (ballSize))
						&& (event.getY() + ballSize < p2Pos.y) && !gameplay) {
					mTouchX = (int) event.getX();
					mTouchY = (int) event.getY();
				} else if (event.getY() > heightHalf && gameplay) {
					mTouchX = -1;
					mTouchY = -1;
					nTouchX = (int) event.getX();
				}
				if ((event.getY() > heightHalf)
						&& (event.getY() > (height20) + (ballSize))
						&& (event.getY() + ballSize < p2Pos.y)
						&& event.getX() > 1 && event.getX() < width
						&& !gameplay) {
					this.myYDirection = VerticalDirection.UP;
				} else if ((event.getY() < heightHalf)
						&& (event.getY() > (height20) + (ballSize))
						&& (event.getY() + ballSize < p2Pos.y)
						&& event.getX() > 1 && event.getX() < width
						&& !gameplay) {
					this.myYDirection = VerticalDirection.DOWN;
				}
			} else {
				mTouchX = -1;
				mTouchY = -1;
				nTouchX = -1;
			}

			notify();
		}
	}

	public void pausePainting() {
		this.wait = true;
		synchronized (this) {
			this.notify();
		}
	}

	public void resumePainting() {
		this.wait = false;
		synchronized (this) {
			this.notify();
			String timeSetting = Settings.System.getString(context
					.getContentResolver(), Settings.System.TIME_12_24);
			this.military = (timeSetting == null) ? false : (timeSetting
					.equals("12") ? false : true);
			cal = Calendar.getInstance();
			time = sdf.format(cal.getTime());
			minute = Integer.parseInt(time.substring(3, 5));
			hour = (military) ? Integer.parseInt(time.substring(0, 2))
					: (Integer.parseInt(time.substring(0, 2))) % 12;
			if (hour == 0 && !military)
				hour = 12;
			this.dateString = date.format(cal.getTime());
			this.score1 = hour;
			this.score2 = minute;
			display = ((WindowManager) context
					.getSystemService(Context.WINDOW_SERVICE))
					.getDefaultDisplay();
			color = mPrefs.getString("pongur_color", "black");
			this.gameplay = mPrefs.getBoolean("gameplay", false);
			this.showclock = mPrefs.getBoolean("showclock", true);
			this.showShadow = mPrefs.getBoolean("showshadow", false);
			this.showDash = mPrefs.getBoolean("showdash", true);
			String speed = mPrefs.getString("pongur_speed", "2");
			setSpeed(speed);
			this.width = display.getWidth();
			this.height = display.getHeight();
			this.widthHalf = width / 2;
			this.widthQuarter = widthHalf / 2;
			this.heightHalf = height / 2;
			this.height20 = height / 20;
			this.mColorDiv = (int) (Math.floor((width / 255)) + 1);
			if (mColorDiv < 2)
				mColorDiv = 2;
			this.paddlespeed = (speedChoice + 4) + width / 24;
			this.ballSize = width / 20;
			this.paddleWidth = ballSize * 3;
			this.paddleHalf = paddleWidth / 2;
			this.ballHalf = ballSize / 2;
			this.ballQuarter = ballHalf / 2;
			this.bgColor = 0xffffffff;
			this.bShape = (mPrefs.getString("pongur_shape", "cube")
					.equals("cube")) ? 0 : 1;
			ballPos = new Point(((widthHalf) - ballHalf), heightHalf);
			p1Pos = new Point(widthHalf, height20);
			p2Pos = new Point(widthHalf, height - ((height20) + (height / 15)));
			this.textPos.x = widthHalf;
			this.textPos2.x = widthHalf;
			setColor(color);
			if (bShape == 0) {
				paint.setTypeface(face);
				paint.setTextSize(48);
				datePaint.set(paint);
				datePaint.setTextSize(14);
			} else {
				paint.setTypeface(Typeface.MONOSPACE);
				paint.setTextSize(72);
				datePaint.set(paint);
				datePaint.setTextSize(20);
			}
			datePaint.setColor(paint.getColor());
			String scorePos = mPrefs.getString("pongur_scorepos", "two");
			setScorePos(scorePos);
		}
	}

	@Override
	public void run() {
		this.run = true;
		Canvas c = null;
		while (run) {
			try {
				c = this.surfaceHolder.lockCanvas(surfaceHolder
						.getSurfaceFrame());
				synchronized (this.surfaceHolder) {
					try {
						surfaceHolder.wait(37);
					} catch (InterruptedException e) {
						e.printStackTrace();
					} finally {
						currentTime = System.currentTimeMillis();
						updatePhysics();
						doDraw(c);
						previousTime = currentTime;
					}
				}
			} finally {
				if (c != null) {
					this.surfaceHolder.unlockCanvasAndPost(c);
				}
			}
			// pause if no need to animate
			synchronized (this) {
				if (wait) {
					try {
						this.wait();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	public void setColor(String color) {
		if (color.equals("black")) {
			this.bColor = 0xFF000000;
			paint.setARGB(255, 255, 255, 255);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(3, 0, 0, 0xeeffffff);
				datePaint.setShadowLayer(3, 0, 0, 0xeeffffff);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("white")) {
			this.bColor = 0xFFffffff;
			paint.setARGB(255, 0, 0, 0);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(2, 1, 1, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("redyellow")) {
			this.bColor = 0xFFff0000;
			paint.setARGB(255, 255, 255, 0);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("redblack")) {
			this.bColor = 0xFF000000;
			paint.setARGB(255, 168, 0, 0);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(3, 0, 0, paint.getColor());
				datePaint.setShadowLayer(2, 0, 0, paint.getColor());
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("android")) {
			this.bColor = 0xFFffffff;
			paint.setARGB(255, 164, 198, 57);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("purplegreen")) {
			this.bColor = 0xFF410052;
			paint.setARGB(255, 0, 250, 50);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("surf")) {
			this.bColor = 0xFF9cc8ff;
			paint.setARGB(255, 234, 230, 155);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("orangeblue")) {
			this.bColor = 0xFF201dde;
			paint.setARGB(255, 222, 148, 29);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("bluewhite")) {
			this.bColor = 0xFFffffff;
			paint.setARGB(255, 19, 19, 235);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("pinkblack")) {
			this.bColor = 0xFF000000;
			paint.setARGB(255, 255, 126, 180);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(3, 0, 0, paint.getColor());
				datePaint.setShadowLayer(2, 0, 0, paint.getColor());
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("yellowblack")) {
			this.bColor = 0xFF000000;
			paint.setARGB(255, 255, 234, 0);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(3, 0, 0, paint.getColor());
				datePaint.setShadowLayer(2, 0, 0, paint.getColor());
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("greenblack")) {
			this.bColor = 0xFF000000;
			paint.setARGB(255, 0, 255, 0);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(3, 0, 0, paint.getColor());
				datePaint.setShadowLayer(2, 0, 0, paint.getColor());
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("egg")) {
			this.bColor = 0xFFff91c8;
			paint.setARGB(255, 85, 143, 201);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("blue")) {
			this.bColor = 0xFF0000ff;
			paint.setARGB(255, 204, 204, 204);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("shadow")) {
			this.bColor = 0xFFffffff;
			if (showShadow) {
				paint.setARGB(0, 122, 122, 122);
				paint.setStyle(Style.FILL);
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setARGB(255, 122, 122, 122);
				paint.setStyle(Style.FILL);
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}

		} else if (color.equals("corn")) {
			this.bColor = 0xFF6495ed;
			paint.setARGB(255, 217, 237, 100);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("florence")) {
			this.bColor = 0xFF61abff;
			paint.setARGB(255, 194, 78, 74);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("cyanogen")) {
			this.bColor = 0xFF373737;
			paint.setARGB(255, 24, 255, 255);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("osc")) {
			this.bColor = 0xFF247562;

			paint.setStyle(Style.STROKE);
			paint.setStrokeWidth(2);
			if (showShadow) {
				paint.setARGB(0, 29, 255, 29);
				paint.setShadowLayer(1, 0, 0, Color.argb(255, 29, 255, 29));
				datePaint.setShadowLayer(1, 0, 0, Color.argb(255, 29, 255, 29));
			} else {
				paint.setARGB(255, 29, 255, 29);
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("apr")) {
			paint.setARGB(255, 255, 255, 0);
			paint.setStyle(Style.STROKE);
			paint.setShadowLayer(0, 0, 0, 0);
			datePaint.setShadowLayer(0, 0, 0, 0);
			paint.setStrokeWidth(2);
			ballSize = (Math.abs(ballPos.y - (heightHalf)) / 8) + 4;
			ballHalf = ballSize / 2;
			bgColor = Color.rgb(
					135 - ((Math.abs(p2Pos.x - (widthHalf) / 6) - tap2 / 3)),
					0,
					120 - ((Math.abs(p1Pos.x - (widthHalf)) / 10) - tap2 / 3));
		} else if (color.equals("b")) {
			if (mColorDiv < 2)
				mColorDiv = 2;
			paint.setARGB(255, 255, 236, 255);
			paint.setStyle(Style.FILL);
			bgColor = Color.rgb(p1Pos.x / mColorDiv, (ballPos.x + 37)
					/ mColorDiv, p2Pos.x / mColorDiv);
			if (showShadow) {
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("maraqua")) {
			this.bColor = 0xFF10b0b0;
			paint.setARGB(255, 176, 16, 80);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else if (color.equals("redwhite")) {
			this.bColor = 0xFFFFFFFF;
			paint.setARGB(255, 255, 0, 0);
			paint.setStyle(Style.FILL);
			if (showShadow) {
				paint.setShadowLayer(2, 2, 2, 0x99000000);
				datePaint.setShadowLayer(1, 0, 0, 0x99000000);
			} else {
				paint.setShadowLayer(0, 0, 0, 0);
				datePaint.setShadowLayer(0, 0, 0, 0);
			}
		} else {
			this.bColor = 0xFF000000;
			paint.setARGB(255, 255, 255, 255);
			paint.setStyle(Style.FILL);
		}

	}

	public void setGamePlay(boolean gameplay) {
		this.gameplay = gameplay;
	}

	public void setScorePos(String scorepos) {

		if (display.getOrientation() % 2 > 0) {
			if (scorepos.equals("one") || scorepos.equals("two")) {
				this.textPos.y = height / 4;
				this.textPos2.y = textPos.y + (height / 12);
			} else if (scorepos.equals("three") || scorepos.equals("four")) {
				this.textPos.y = 3 * (height / 4);
				this.textPos2.y = textPos.y + (height / 12);
			}

		} else {

			if (scorepos.equals("one")) {
				if (height <= 480) {
					this.textPos.y = 96;
					this.textPos2.y = 108;
				} else if (height <= 800) {
					this.textPos.y = 124;
					this.textPos2.y = 164;
				} else if (height <= 854) {
					this.textPos.y = 124;
					this.textPos2.y = 164;
				}

			} else if (scorepos.equals("two")) {
				if (height <= 480) {
					this.textPos.y = 192;
					this.textPos2.y = 206;
				} else if (height <= 800) {
					this.textPos.y = 300;
					this.textPos2.y = 340;
				} else if (height > 800) {
					this.textPos.y = 316;
					this.textPos2.y = 356;
				}
			} else if (scorepos.equals("three")) {
				if (height <= 480) {
					this.textPos.y = 288;
					this.textPos2.y = 300;
				} else if (height <= 800) {
					this.textPos.y = 484;
					this.textPos2.y = 524;
				} else if (height > 800) {
					this.textPos.y = 508;
					this.textPos2.y = 548;
				}

			} else if (scorepos.equals("four")) {
				if (height <= 480) {
					this.textPos.y = 384;
					this.textPos2.y = 396;
				} else if (height <= 800) {
					this.textPos.y = 628;
					this.textPos2.y = 668;
				} else if (height > 800) {
					this.textPos.y = 700;
					this.textPos2.y = 740;
				}
			}
		}

	}

	public void setShape(int bShape) {
		this.bShape = bShape;
	}

	public void setShowClock(boolean showclock) {
		this.showclock = showclock;
	}

	public void setShowDash(boolean showDash) {
		this.showDash = showDash;
	}

	void setSpeed(String speed) {
		if (speed.equals("1"))
			speedChoice = 2;
		else if (speed.equals("2"))
			speedChoice = 3;
		else if (speed.equals("3"))
			speedChoice = 5;
		else
			speedChoice = 3;

	}

	public void setSurfaceSize(int width, int height) {
		synchronized (this) {
			display = ((WindowManager) context
					.getSystemService(Context.WINDOW_SERVICE))
					.getDefaultDisplay();
			color = mPrefs.getString("pongur_color", "black");
			this.width = display.getWidth();
			this.height = display.getHeight();
			this.widthHalf = width / 2;
			this.widthQuarter = widthHalf / 2;
			this.heightHalf = height / 2;
			this.height20 = height / 20;
			this.mColorDiv = (int) (Math.floor((width / 255)) + 1);
			if (mColorDiv < 2)
				mColorDiv = 2;
			this.paddlespeed = (speedChoice + 4) + width / 24;
			this.ballSize = width / 20;
			this.paddleWidth = ballSize * 3;
			this.paddleHalf = paddleWidth / 2;
			this.ballHalf = ballSize / 2;
			this.ballQuarter = ballHalf / 2;
			this.bgColor = 0xffffff;
			setColor(color);
			ballPos = new Point(((widthHalf) - ballHalf), heightHalf);
			p1Pos = new Point(widthHalf, height20);
			p2Pos = new Point(widthHalf, height - ((height20) + (height / 15)));
			this.textPos.x = widthHalf;
			this.textPos2.x = widthHalf;
			this.ball = new Rect(ballPos.x, ballPos.y, ballPos.x + ballSize,
					ballPos.y + ballSize);
			this.p1 = new Rect(p1Pos.x, p1Pos.y, p1Pos.x + paddleWidth, p1Pos.y
					+ (ballSize));
			this.p2 = new Rect(p2Pos.x, p2Pos.y, p2Pos.x + paddleWidth, p2Pos.y
					+ (ballSize));
			String scorePos = mPrefs.getString("pongur_scorepos", "two");
			setScorePos(scorePos);
			dashPath = new Path();
			for (int i = 0; i < width; i += width / 30) {
				dashPath.addRect((((i) * 2) + (width / 48) / 2),
						((heightHalf) - ballHalf), (((i) * 2) + (width / 24)),
						(heightHalf) + ballHalf, Path.Direction.CW);
			}

			this.notify();
		}
	}

	public void stopPainting() {
		this.run = false;
		synchronized (this) {
			this.notify();
		}
	}

	private void updatePhysics() {
		// if nothing was updated :
		// this.wait = true;
	}

	public void setShowShadow(boolean showshadow) {
		this.showShadow = showshadow;
	}

}
